//
//  DABookTypeViewController.m
//  DAKnowledge
//
//  Created by DreamArts on 16/5/13.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DABookTypeViewController.h"
#import "DABookTypeTableViewCell.h"
#import "DASearchBookViewController.h"
@interface DABookTypeViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate>
{
    UIView *statusCover;
}
@property (weak, nonatomic) IBOutlet UIView *searchBarBackView;
@property (weak, nonatomic) IBOutlet UISearchBar *search;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *typeArray;
@property (strong, nonatomic) IBOutlet UISearchDisplayController *searchDisplayController;


@end

@implementation DABookTypeViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  //  [self prepareData];
    [self searchSetting];
    [self tableViewSetting];
    
}
#pragma mark -tableView 设置
- (void)tableViewSetting{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 44;
    self.tableView.tableFooterView = [UIView new];
    
}
#pragma mark - 数据准备
- (void)prepareData{
    NSString *url = DABookTypeURL;
    [DADataHandel postURL:url parame:nil cookie:nil result:^(id result) {
        if (result) {
            self.typeArray = result[@"result"];
            [self.tableView reloadData];
        } else {
            NSLog(@"DABookTypeURL 数据准备失败。");
        }
    }];
}
#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableView) {
        return self.typeArray.count;
    }else{
        return 5;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
        DABookTypeTableViewCell *bookType = [tableView dequeueReusableCellWithIdentifier:@"booktype"];
        bookType.textLabel.text = self.typeArray[indexPath.row][@"bookTypeName"];
        bookType.textLabel.font = [UIFont systemFontOfSize:14];
        return bookType;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        return cell;
        
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableView) {
//        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//        DASearchBookViewController *searchBook = [story instantiateViewControllerWithIdentifier:@"search"];
//        searchBook.typeID = self.typeArray[indexPath.row][@"bookTypeId"];
//        [self.navigationController pushViewController:searchBook animated:YES];
    }else{
        
    }
    
}
#pragma mark - searchBar设置
- (void)searchSetting{
    self.search.backgroundImage = [self imageWithColor:[UIColor clearColor] size:self.search.bounds.size];
    self.search.placeholder = @"请输入书名或编号";
     self.search.delegate = self;
    self.searchDisplayController.delegate = self;
    UITextField *text = [self.search valueForKey:@"_searchField"];
    [text setValue:[UIFont systemFontOfSize:12] forKeyPath:@"_placeholderLabel.font"];
    self.searchBarBackView.backgroundColor = [UIColor colorWithHexString:@"#F3F0ED"];
    [self.search setTintColor:[UIColor whiteColor]];
   // [self.search setBarTintColor:[UIColor supportGreenColor]];
   // [self.search setTintColor:[UIColor whiteColor]];
    
    [[[[self.searchDisplayController.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
    
    
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [self.searchDisplayController.searchBar setBackgroundColor:[UIColor supportGreenColor]];
    statusCover = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 20)];
    statusCover.backgroundColor = [UIColor supportGreenColor];
    [self.view addSubview:statusCover];
    [self.search setTintColor:[UIColor whiteColor]];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    [self.searchDisplayController.searchBar setBackgroundColor:[UIColor colorWithHexString:backGroundColor]];
    [statusCover removeFromSuperview];
    [self.search setTintColor:[UIColor darkGrayColor]];
}



#pragma mark - searchBar背景颜色
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
